/*******************************************************************************
 *
 *    Copyright (c) Baina Info Tech Co. Ltd
 *
 *    Timer TurnOff
 *    
 *    OperationManager
 *    TODO File description or class description.
 *
 *    @author: zuochen
 *    @since:  2011-10-9
 *    @version: 1.0
 *
 ******************************************************************************/

package com.sdk.hardware.manager;

import java.lang.reflect.Method;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.provider.Settings;


/**
 * OperationManager of Timer TurnOff.
 * 
 * @author zuochen
 * 
 */

public class OperationManager {
	Context mContext;
	private static OperationManager mOperationManager;
	WifiManager mWifiManager;

	private OperationManager(Context context) {
		this.mContext = context;
		mWifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
	}

	public static OperationManager getInstance(Context context) {
		if (mOperationManager == null) {
			mOperationManager = new OperationManager(context);
		}
		return mOperationManager;
	}

	/**
	 * 开启飞行模式
	 */
	public void turnOnAirplanMode() {
		final ContentResolver localContentResolver = mContext
				.getContentResolver();
		final String str = Settings.System.AIRPLANE_MODE_ON;
		Settings.System.putInt(localContentResolver, str, 1);
		final Intent localIntent = new Intent(
				Intent.ACTION_AIRPLANE_MODE_CHANGED);
		mContext.sendBroadcast(localIntent);
	}

	/**
	 * 关闭飞行模式
	 */
	public void turnOffAirplanMode() {
		final ContentResolver localContentResolver = mContext
				.getContentResolver();
		final String str = Settings.System.AIRPLANE_MODE_ON;
		Settings.System.putInt(localContentResolver, str, 0);
		final Intent localIntent = new Intent(
				Intent.ACTION_AIRPLANE_MODE_CHANGED);
		mContext.sendBroadcast(localIntent);
	}

	/**
	 * 关闭wifi
	 */
	public void turnOffWifi() {
		if (null != mWifiManager) {
			mWifiManager.setWifiEnabled(false);
		}
	}

	/**
	 * 开启wifi
	 */
	public void turnOnfWifi() {
		mWifiManager.setWifiEnabled(false);
		mWifiManager.setWifiEnabled(true);
	}


	/**
	 * 获取wifi当前的状态
	 * @return
	 */
	public int getWifiApState() {
		try {
			final Method method = mWifiManager.getClass().getMethod(
					"getWifiApState");
			return (Integer) method.invoke(mWifiManager);
		} catch (final Exception e) {
			return 0;
		}
	}
	/**
	 * 开启蓝牙
	 */
	public void turnOnBlueTooth(){
		BluetoothManager.turnOnBlueTooth(mContext);
	}
	/**
	 * 关闭蓝牙
	 */
	public void turnOffBlueTooth(){
		BluetoothManager.turnOffBlueTooth(mContext);
	}
}
